<template lang="">
  <div>
    <el-dialog
      v-model="currShow"
      :title="editClassRoomInfo ? '编辑教室' : '新增教室'"
      width="500px"
      :before-close="close"
      :close-on-click-modal="false"
      @open="open()"
    >
      <el-form
        :model="queryForm"
        v-loading="formLoading"
        :rules="rules"
        ref="queryRef"
        label-width="100px"
        class="demo-form-inline"
      >
        <el-row>
          <el-col :span="23">
            <el-form-item label="教室名称" prop="name">
              <el-input v-model.trim="queryForm.name" maxlength="20" :disabled="editClassRoomInfo ? true : false" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="地点" prop="addRess">
              <el-input v-model.trim="queryForm.addRess" maxlength="50" :disabled="editClassRoomInfo ? true : false" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="备注" prop="cur_statusCode">
              <el-input v-model="queryForm.remarks" :rows="4" maxlength="300" show-word-limit type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close(false)">取消</el-button>
          <el-button type="primary" @click="submit(queryRef)" :loading="submitLoading">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { addClassRoom, editClassRoom } from '@/api/teaching/class-room'
import { nextTick } from 'vue'
import useCurrentInstance from '@/utils/useCurrentInstance'
import { defineComponent, reactive, ref, watch } from '@vue/runtime-core'
import { ElMessage } from 'element-plus'
export default defineComponent({
  props: {
    valueShow: {
      type: Boolean,
      default: false,
    },
    editClassRoomInfo: {
      type: Object,
      default: () => { },
    },
  },
  setup(props, { emit }) {
    // 变量区域
    const { proxy } = useCurrentInstance()
    const currShow = ref(false)
    const formLoading = ref(false)
    const submitLoading = ref(false)
    const queryRef: any = ref(null)
    const queryForm: any = ref({
      name: '',
      addRess: '',
      remarks: '',
    })
    const rule = { required: true, message: '必填项', trigger: 'blur' }
    const rules = {
      name: [rule],
      addRess: [rule],
    }

    // 方法区域
    const open = () => {
      if (props.editClassRoomInfo) {
        queryForm.value = { ...props.editClassRoomInfo }
        queryForm.value.name = props.editClassRoomInfo.class_room
      }
    }

    const close = (val) => {
      emit('closeClassRoom', val)
      nextTick(() => {
        queryRef.value.resetFields()
      })
    }

    const submit = (formRef) => {
      formRef.validate((valid) => {
        if (!valid) return
        formLoading.value = true
        submitLoading.value = true
        const submitApi = props.editClassRoomInfo ? editClassRoom : addClassRoom
        submitApi(queryForm.value).then((res: any) => {
          ElMessage.success(res.message)
          close(true)
        }).finally(() => {
          formLoading.value = false
          submitLoading.value = false
        })
      })
    }

    const validateNumber = (obj, val) => {
      // 用正则表达式检查输入是否合法
      const regex = /^\d*\.?\d{0,2}$/
      // 如果输入不合法，移除最后一位字符
      !regex.test(obj[val]) && (obj[val] = obj[val].slice(0, -1))
    }

    // 钩子函数区域
    watch(
      () => props.valueShow,
      (newVal) => {
        currShow.value = newVal
      }
    )

    // 方法回调区域

    // return返回值
    return {
      currShow,
      formLoading,
      submitLoading,
      queryRef,
      queryForm,
      rules,
      open,
      close,
      submit,
      validateNumber,
    }
  },
})
</script>
<style scoped lang="scss"></style>
